home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / console / splitvt-.000 / splitvt- / splitvt-1.6.1 / terminal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-04  |  8.3 KB  |  422 lines

  1.  
  2. /* This is the upper layer of the splitvt terminal driver.
  3.    It contains the termcap stuff and the output routines that 
  4.    actually write to the terminal.
  5. */
  6.  
  7.  
  8. #include    <stdio.h>
  9. #include    "video.h"
  10. #include    "terminal.h"
  11.  
  12.  
  13. /* This is a long list of termcap capabilities that can be used by splitvt */
  14.  
  15. typedef struct {
  16.     char *name;
  17.     char *value;
  18.     int   required;
  19.     } capability;
  20.  
  21. static capability capabilities[] = {
  22. #define cs 0
  23.     { "cs", NULL, 0 },        /* Scrolling regions: VITAL! */
  24. #define cm 1
  25.     { "cm", NULL, 1 },        /* Cursor move to coordinates */
  26. #define sc 2
  27.     { "sc", NULL, 0 },        /* Save cursor position */
  28. #define rc 3
  29.     { "rc", NULL, 0 },        /* Restore cursor position */
  30. #define sr 4
  31.     { "sr", NULL, 1 },        /* Reverse scroll 1 line */
  32. #define sf 5
  33.     { "sf", "\n", 1 },        /* Forward scroll 1 line */
  34. #define UP 6
  35.     { "UP", NULL, 0 },        /* Move cursor up n lines */
  36. #define up 7
  37.     { "up", NULL, 0 },        /* Move cursor up 1 line */
  38. #define DO 8
  39.     { "DO", NULL, 0 },        /* Move cursor down n lines */
  40. #define do 9
  41.     { "do", NULL, 0 },        /* Move cursor down 1 line */
  42. #define RI 10
  43.     { "RI", NULL, 0 },        /* Move cursor right n characters */
  44. #define nd 11
  45.     { "nd", NULL, 0 },        /* Move cursor right 1 character */
  46. #define LE 12
  47.     { "LE", NULL, 0 },        /* Move cursor left n characters */
  48. #define le 13
  49.     { "le", "\b", 0 },        /* Move cursor left 1 character */
  50. #define cr 14
  51.     { "cr", "\r", 1 },        /* Carriage return */
  52. #define DC 15
  53.     { "DC", NULL, 0 },        /* Delete n characters */
  54. #define dc 16
  55.     { "dc", NULL, 0 },        /* Delete character */
  56. #define DL 17
  57.     { "DL", NULL, 0 },        /* Delete n lines */
  58. #define dl 18
  59.     { "dl", NULL, 0 },        /* Delete line */
  60. #define ce 19
  61.     { "ce", NULL, 1 },        /* Clear to end of line */
  62. #define so 20
  63.     { "so", NULL, 0 },        /* Start standout text mode */
  64. #define se 21
  65.     { "se", NULL, 0 },        /* End standout text mode */
  66. #define us 22
  67.     { "us", NULL, 0 },        /* Start underline text mode */
  68. #define ue 23
  69.     { "ue", NULL, 0 },        /* End underline text mode */
  70. #define md 24
  71.     { "md", NULL, 0 },        /* Start bold text mode */
  72. #define mb 25
  73.     { "mb", NULL, 0 },        /* Start blinking text mode */
  74. #define mr 26
  75.     { "mr", NULL, 0 },        /* Start reverse text mode */
  76. #define mh 27
  77.     { "mh", NULL, 0 },        /* Start dim text mode */
  78. #define me 28
  79.     { "me", NULL, 0 },        /* Clear all text attributes */
  80. #define ZF 29
  81.     { "ZF", NULL, 0 },        /* Set wide (132 column) mode */
  82. #define IC 30
  83.     { "IC", NULL, 0 },        /* Insert character */
  84.     { NULL, NULL, 0 }
  85.     };
  86.  
  87. #ifdef TERMCAP    /* Termcap versions of the screen update routines */
  88.  
  89. /* areas and variables used to get termcap information */
  90.  
  91. extern int tgetent();
  92. extern char *tgetstr();
  93.  
  94. static char termcap_entry[2048];        /* termcap entry for the terminal */
  95. static char termcap_area[2048];        /* Static buffer for capabilities */
  96. static char *tp=termcap_area;        /* Used in tgetent() calls */
  97. static char *terminal_type;             /* type of terminal */
  98.  
  99. char *termcap_init(termtype)
  100. char *termtype;
  101. {
  102.     static char errmesg[BUFSIZ];
  103.     char *value;
  104.     int i;
  105.  
  106.     /* Determine the terminal type and get the termcap entry */
  107.     if ( tgetent(termcap_entry, termtype) < 1 ) {
  108.                 sprintf(errmesg,
  109.         "Can't find termcap entry for terminal type \"%s\"", terminal_type);
  110.                 return(errmesg);
  111.         }
  112.  
  113.     /* Make sure this terminal has all the required capabilities */    
  114.     for ( i=0; capabilities[i].name; ++i ) {
  115.         if ( (value=tgetstr(capabilities[i].name, &tp)) == NULL ) {
  116.             if ( !capabilities[i].value && 
  117.                         capabilities[i].required ) {
  118.                 sprintf(errmesg, 
  119.             "Your terminal lacks the %s termcap capability.",
  120.                         capabilities[i].name);
  121.                 return(errmesg);
  122.             }
  123.         } else
  124.             capabilities[i].value=value;
  125.     }
  126.     return(NULL);
  127. }
  128. void outc(c) int c; { putchar(c); }
  129.  
  130. void vt_rows_cols(termtype, rows, cols)
  131. char *termtype;
  132. int *rows;
  133. int *cols;
  134. {
  135.     if ( rows ) {
  136.         if ( (*rows=tgetnum("li")) <= 0 )
  137.             *rows=24;
  138.     }
  139.     if ( cols ) {
  140.         if ( (*cols=tgetnum("co")) <= 0 )
  141.             *cols=80;
  142.     }
  143. }
  144. void vt_bold(on)
  145. int on;
  146. {
  147.     if ( on && capabilities[md].value ) 
  148.         tputs(capabilities[md].value, 1, outc);
  149.     else
  150.         printf("\033[%sm", (on ? "1" : "22"));
  151. }
  152. void vt_underline(on)
  153. int on;
  154. {
  155.     if ( capabilities[on ? us : ue].value ) 
  156.         tputs(capabilities[on ? us : ue].value, 1, outc);
  157.     else
  158.         printf("\033[%sm", on ? "4" : "24");
  159. }
  160. void vt_blink(on)
  161. int on;
  162. {
  163.     if ( on && capabilities[mb].value ) 
  164.         tputs(capabilities[mb].value, 1, outc);
  165.     else
  166.         printf("\033[%sm", on ? "5" : "25");
  167. }
  168. void vt_reverse(on)
  169. int on;
  170. {
  171.     if ( on && capabilities[mr].value ) 
  172.         tputs(capabilities[mr].value, 1, outc);
  173.     else
  174.         printf("\033[%sm", on ? "7" : "27");
  175. }
  176. void vt_resetattr()
  177. {
  178.     if ( capabilities[me].value ) 
  179.         tputs(capabilities[me].value, 1, outc);
  180.     else
  181.         printf("\033[m");
  182. }
  183. void vt_widemode(on)
  184. int on;
  185. {
  186.     if ( on && capabilities[ZF].value ) 
  187.         tputs(capabilities[ZF].value, 1, outc);
  188.     else
  189.         printf("\033[?3%c", on ? 'h' : 'l');
  190. }
  191. void vt_savecursor()
  192. {
  193.     if ( capabilities[sc].value )
  194.         tputs(capabilities[sc].value, 1, outc);
  195.     else
  196.         printf("\0337");
  197. }
  198. void vt_restcursor()
  199. {
  200.     if ( capabilities[rc].value )
  201.         tputs(capabilities[rc].value, 1, outc);
  202.     else
  203.         printf("\0338");
  204. }
  205. #else
  206. char *termcap_init(termtype)
  207. char *termtype;
  208. {
  209.     return(NULL);
  210. }
  211. void vt_rows_cols(termtype, rows, cols)
  212. char *termtype;
  213. int *rows;
  214. int *cols;
  215. {
  216.     if ( cols ) {
  217.         if ( strcmp("vt100-w", termtype) == 0 )
  218.         { /* vt100-wide terminal (132 columns) */
  219.             *cols=132;
  220.         } else
  221.             *cols = 80;
  222.     } 
  223.     if ( rows )
  224.         *rows = 24;
  225. }
  226. void vt_bold(on)
  227. int on;
  228. {
  229.     printf("\033[%sm", (on ? "1" : "22"));
  230. }
  231. void vt_underline(on)
  232. int on;
  233. {
  234.     printf("\033[%sm", on ? "4" : "24");
  235. }
  236. void vt_blink(on)
  237. int on;
  238. {
  239.     printf("\033[%sm", on ? "5" : "25");
  240. }
  241. void vt_reverse(on)
  242. int on;
  243. {
  244.     printf("\033[%sm", on ? "7" : "27");
  245. }
  246. void vt_resetattr()
  247. {
  248.     printf("\033[m");
  249. }
  250. void vt_widemode(on)
  251. int on;
  252. {
  253.     printf("\033[?3%c", on ? 'h' : 'l');
  254. }
  255. void vt_savecursor()
  256. {
  257.     printf("\0337");
  258. }
  259. void vt_restcursor()
  260. {
  261.     printf("\0338");
  262. }
  263. #endif
  264.         /* vt100 compatible versions of the screen update routines */
  265. char *vt_initterm(terminal_type, rows, cols)
  266. char *terminal_type;
  267. int *rows;
  268. int *cols;
  269. {
  270.     extern char *getenv();
  271.     char *termtype, *error=NULL;
  272.  
  273.         if ( ((termtype=getenv("TERM")) == NULL ||
  274.          (strncmp(termtype, "vt10x", 4)) != 0) &&
  275.                         /* A vt10x emulation detector -->  */   ! vttest() )
  276.                 return("Terminal type must be set to vt100");
  277.  
  278.     /* Get extra (possibly) capabilities from termcap */
  279.     if ( (error=termcap_init(termtype)) != NULL )
  280.         return(error);
  281.  
  282.     vt_rows_cols(termtype, rows, cols);
  283.     if ( terminal_type )
  284.         strcpy(terminal_type, termtype);
  285.     return(NULL);
  286. }
  287. void vt_bell()
  288. {
  289.     printf("\007");
  290. }
  291. void vt_goto(row, col)
  292. int row, col;
  293. {
  294.     printf("\033[%d;%dH", row, col);
  295. }
  296. void vt_up(numrows)
  297. int numrows;
  298. {
  299.     printf("\033[%dA", numrows);
  300. }
  301. void vt_down(numrows)
  302. int numrows;
  303. {
  304.     printf("\033[%dB", numrows);
  305. }
  306. void vt_right(numcols)
  307. int numcols;
  308. {
  309.     printf("\033[%dC", numcols);
  310. }
  311. void vt_left(numcols)
  312. int numcols;
  313. {
  314.     printf("\033[%dD", numcols);
  315. }
  316. void vt_clrscr()
  317. {
  318.     printf("\033[2J");
  319. }
  320. void vt_clreos()
  321. {
  322.     printf("\033[J");
  323. }
  324. void vt_clrbgs()
  325. {
  326.     printf("\033[1J");
  327. }
  328. void vt_clrline()
  329. {
  330.     printf("\033[2K");
  331. }
  332. void vt_clreol()
  333. {
  334.     printf("\033[K");
  335. }
  336. void vt_clrbgl()
  337. {
  338.     printf("\033[1K");
  339. }
  340. void vt_delunder(num)
  341. int num;
  342. {
  343.     printf("\033[%dP", num);
  344. }
  345. void vt_delline(num)
  346. int num;
  347. {
  348.     printf("\033[%dM", num);
  349. }
  350. void vt_insline(num)
  351. int num;
  352. {
  353.     printf("\033[%dL", num);
  354. }
  355. void vt_setattr(textattr)
  356. int textattr;
  357. {
  358.     vt_resetattr();
  359.     
  360.     if ( textattr&BOLD )
  361.         vt_bold(1);
  362.     if ( textattr&UNDERLINE )
  363.         vt_underline(1);
  364.     if ( textattr&BLINK )
  365.         vt_blink(1);
  366.     if ( textattr&REVERSE )
  367.         vt_reverse(1);
  368. }
  369. void vt_setfg(color)
  370. int color;
  371. {
  372.     printf("\033[%dm", color+30);
  373. }
  374. void vt_setbg(color)
  375. int color;
  376. {
  377.     printf("\033[%dm", color+40);
  378. }
  379. void vt_altcharset(charset, type)
  380. int charset;
  381. int type;
  382. {
  383.     switch (type) {
  384.         case UK_CHARSET:
  385.                 printf("\033%cA", (charset == G0 ? '(' : ')'));
  386.                 break;
  387.         case US_CHARSET:
  388.                 printf("\033%cB", (charset == G0 ? '(' : ')'));
  389.                 break;
  390.         case GRAPHICS:    
  391.                 printf("\033%c0", (charset == G0 ? '(' : ')'));
  392.                 break;
  393.         default:    break;
  394.     }
  395. }
  396. void vt_setscroll(upper, lower)
  397. int upper, lower;
  398. {
  399.     printf("\033[%d;%dr", upper, lower);
  400. }
  401. void vt_revscroll()
  402. {
  403.     printf("\033M");
  404. }
  405. void vt_keystate(application)
  406. int application;
  407. {
  408.     /* Set and reset the numeric keypad and arrowkeys */
  409.     if ( application )
  410.         printf("\033=\033[?1h");
  411.     else
  412.         printf("\033>\033[?1l");
  413. }
  414. void vt_insertchar(numcols)
  415. {
  416.     printf("\033[%d@", numcols);
  417. }
  418. void vt_update()
  419. {
  420.     fflush(stdout);
  421. }
  422.